/*
var frm = document.getElementsByTagName('form');

for (var i = 0; i < frm.length; i++) {
	frm[i].style.display = 'none !important';
}*/
chrome.extension.sendRequest({'action' : 'insertCss'});
//$(document).ready(function() {
	//alert(ShowTippingDiv);
	var Chapter = function(url, callback) {
		var chap = {
			'bookid' : null,
			'chapid' : null,
			'pageid' : null,
			'authorid' : null,
			'vip' : false,
			'last' : false,
			'first' : false,
			'ok' : null,
			'content' : null,
			'bookname' : null,
			'chapname' : null,
			'author' : null,
			'previd' : null,
			'nextid' : null,
			'book' : null,
			'list' : null,
			'nextchap' : null,
			'nextok' : false,
			draw : function () {
				helper.tpl('chap.html', {
					'chapname' : this.chapname,
					'bookid' : this.bookid,
					'previd' : this.previd,
					'nextid' : this.nextid,
					'chapid' : this.chapid,
					'content' : this.content,
					
				}, function(html) {
					$('#all_chap').append(html);
					if (!chap.last) {
						chap.loadNext();
					}
				});
			},
			drawLast : function() {
				helper.tpl('over.html', {
					'bookname' : this.bookname
				}, function(html) {
					$('#all_chap').append(html);
				});
			},
			loadNext : function() {
				this.nextchap = new Chapter('http://www.qidian.com/BookReader/' + this.bookid + ',' + this.nextid + '.aspx', function(nextchap) {
					console.log('loadnext', nextchap);
					chap.nextok = true;
				});
			}
			
		};
		var reg = url.match(/^.+BookReader\/(\d+),(\d+)\..*$/im);
		if (reg){
			chap.bookid = parseInt(reg[1]);
			chap.chapid = parseInt(reg[2]);
		} else {
			chap.ok = false;
			return chap;
		}
		
		chrome.extension.sendRequest({
			'action' : 'httpGet',
			'url' : url
		}, function(html) {
			var reg = html.match(/bookName:\s*['"](.+?)['"],/im);
			if (!reg) {
				return;
			}
			chap.bookname = reg[1];
			
			reg = html.match(/authorName:\s*['"](.+?)['"],/im);
			if (!reg) {
				return;
			}
			chap.author = reg[1];
			
			reg = html.match(/authorId:\s*['"](.+?)['"],/im);
			if (!reg) {
				return;
			}
			chap.authorid = reg[1];
			
			reg = html.match(/chapterName:\s*['"](.+?)['"],/im);
			if (!reg) {
				return;
			}
			chap.chapname = reg[1];
			
			reg = html.match(/prevpage=['"]\d+,(\d+)\.aspx['"];/im);
			if (reg) {
				chap.previd = reg[1];
			} else if (html.match(/prevpage=['"]\d+\.aspx['"];/im)) {
				chap.first = true;
				chap.previd = chap.chapid;
			} else {
				return;
			}
			
			
			reg = html.match(/nextpage=['"]\d+,(\d+)\.aspx['"];/im);
			if (reg) {
				chap.nextid = reg[1];
			} else if (html.indexOf("nextpage='/BookReader/LastPage.aspx?bookId")) {
				chap.last = true;	
				chap.nextid = chap.chapid;
			} else {
				return;
			}
			reg = html.match(/<script src='([^'"]+?\.txt)'/im);
			if (reg) {
				chrome.extension.sendRequest({
					'action' : 'httpGet',
					'url' : reg[1] + '?' + Date.parse(new Date()),
				}, function(txt) {
					txt = txt.replace(/^document\.write\(/im, '').replace(/\);$/im, '');
					txt = eval(txt);
					txt = txt.replace(/<a .+?<\/a>/ig, '');
					txt = txt.replace(/[　 ]{2,}/ig, '');
					txt = txt.replace(/<p>/ig, '\n');
					txt = $.trim(txt);
					txt = '<p>' + txt.replace(/\n/ig, '</p><p>') + '</p>';
					chap.content = txt;
					chap.ok = true;
					callback(chap);
				});
			}
		});
		return chap;
	}
	var helper = {
		'clean' : function () {
			var blank = function(){return true;};
			document.onkeyup=blank;
			document.onkeydown=blank;
			document.onkeypress=blank;
			document.onmouseup=blank;
			document.onmousemove=blank;
			document.onmousedown=blank;
			document.onclick=blank;
			document.body.onclick=blank;
			document.oncontextmenu=blank;
			document.body.onselectstart=blank;
			document.onselectstart=blank;
			
			$(document.body).unbind().html('<div id="all_chap"></div>');
		},
		tpl : function (file, bind, callback) {
			chrome.extension.sendRequest({
				'action' : 'getRes',
				'file' : file
			}, function(tpl) {
				var reg;
				for (key in bind) {
					reg = new RegExp('\\$\\{' + key + '\\}\\$', 'ig');
					tpl = tpl.replace(reg, bind[key]);
				}
				callback(tpl);
			});
		},
		scrollListener : function() {
			if ((document.body.scrollHeight - document.body.scrollTop  -  window.innerHeight) < 300) {
				if (now_chap.ok && now_chap.nextok) {
					now_chap = now_chap.nextchap;
					now_chap.draw();
				} else if (now_chap.last) {
					now_chap.drawLast();
					now_chap = {'ok' : false};
				}
			}
		},
		scroller : function(){
			$(document).scroll(this.scrollListener);
		}
	}
	helper.clean();
	var now_chap = new Chapter(location.href, function(chap){
		chap.draw();
	});
	
	helper.scroller();
	
//});